package com.treegrid;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.util.IOHelper;
import com.util.JSONParser;
import com.util.NumUtil;

public class TreegridDataRepo {
	private static List<Item> items = new ArrayList<Item>();
	private static int count;
	
	static {
		initItems();		
		initCount();
	}
	
	private static void initItems() {
		String name = "data.json";
		InputStream is = TreegridDataRepo.class.getResourceAsStream(name);
		
		String json = null;
		try {
			json = IOHelper.readFile(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
		items = JSON.parseArray(json, Item.class);
	}
	
	private static void initCount() {
		count = 1;
		for (Item i : items) {
			int id = NumUtil.parseInt(i.getId(), count);
			if (id > count) {
				count = id;
			}
		}
		count++;
	}
	
	// auto increment id
	public static int getCount() {
		int cur = count;
		count++;
		return cur;
	}
	
	public static List<Item> getItems(String pid, int page, int rows) {
		List<Item> all = getItemByPid(pid);
		
		int from = (page - 1) * rows;
		int length = all.size();
		List<Item> list = new ArrayList<Item>();
		for (int i = from; i < length && rows > 0; i++,rows--) {
			list.add(all.get(i));
		}
		
		return list;
	}
	
	public static List<Item> getItemByPid(String pid) {
		List<Item> list = new ArrayList<Item>();
		
		for (Item i : items) {
			if (i.getParentId().equals(pid)) {
				if (hasChild(i.getId())) {
					i.setState("closed");
				}
				list.add(i);
			}
		}
		
		return list;
	}
	
	public static boolean hasChild(String id) {
		for (Item i : items) {
			if (i.getParentId().equals(id)) {
				return true;
			}
		}
		return false;
	}
	
	public static int getRows() {
		int rows = 0;
		for (Item i : items) {
			if (i.getParentId().equals("0")) {
				rows++;
			}
		}
		return rows;
	}
	
	public static Item getItemByName(String name) {
		for (Item i : items) {
			if (i.getName().equals(name)) {
				return i;
			}
		}
		return null;
	}
	
	public static Item addItem(Map<String, Object> item) {
		Item i = JSONParser.parse(item);
		if (getItemByName(i.getName()) == null) {
			i.setId("" + getCount());
			items.add(i);
			return i;
		}
		
		return null;
	}
	
	public static int getItemById(String id) {
		int length = items.size();
		for (int i = 0; i < length; i++) {
			Item item = items.get(i);
			if (item.getId().equals(id)) {
				return i;
			}
		}
		return -1;
	}
	
	public static Item updateItem(Map<String, Object> item) {
		Item i = JSONParser.parse(item);
		int index = getItemById(i.getId());
		if (index != -1) {
			items.set(index, i);
			return i;
		}
		
		return null;
	}
	
	public static boolean removeItem(String id) {
		int index = getItemById(id);
		if (index != -1) {
			items.remove(index);
			System.out.println("remove item: " + id);
			List<Item> children = getItemByPid(id);
			for (Item i : children) {
				removeItem(i.getId());
			}
			return true;
		}
		
		return false;
	}
	
	public static void main(String[] args) {
		System.out.println(items.size());
	}
}
